﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Lists;
using Excel = Microsoft.Office.Interop.Excel;
using System.Collections;

namespace ListForms
{
    public partial class FrmEditHangHoa : ListForms.FrmBaseEditForm
    {
        public FrmEditHangHoa(VatTuHangHoa VatTuHangHoa)
        {
            InitializeComponent();
            InitializeComponent();
            _DoiTuong = VatTuHangHoa;

            BasicForms.FrmBase.FormatTextEdit(txtGiaMua, BasicForms.TextFormatType.Money);
            BasicForms.FrmBase.FormatTextEdit(txtGiaBan, BasicForms.TextFormatType.Money);
            BasicForms.FrmBase.FormatTextEdit(txtGiaBia, BasicForms.TextFormatType.Money);
            BasicForms.FrmBase.FormatTextEdit(txtChietKhau, BasicForms.TextFormatType.Integer);
        }

        public FrmEditHangHoa(string ten)
        {
            InitializeComponent();
            InitializeComponent();
            _DoiTuong = null;
            txtTen.EditValue = ten;

            BasicForms.FrmBase.FormatTextEdit(txtGiaMua, BasicForms.TextFormatType.Money);
            BasicForms.FrmBase.FormatTextEdit(txtGiaBan, BasicForms.TextFormatType.Money);
            BasicForms.FrmBase.FormatTextEdit(txtGiaBia, BasicForms.TextFormatType.Money);
            BasicForms.FrmBase.FormatTextEdit(txtChietKhau, BasicForms.TextFormatType.Integer);
        }

        protected override string GetMa()
        {
            return this.txtMa.Text.Trim();
        }

        protected override void FillData()
        {
            cmbNhomVatTuHangHoa.DataSource = DanhMucNhomVatTuHangHoa.Filter("LienQuanDenKho = 1").DataView;
            cmbNhomVatTuHangHoa.DisplayColumn = "Ten";
            cmbNhomVatTuHangHoa.ValueColumn = "ID";
            cmbNhomVatTuHangHoa.AddVisibleColumn("Ma", "Mã");
            cmbNhomVatTuHangHoa.AddVisibleColumn("Ten", "Tên");

            if (_DoiTuong == null)
            {
                cmbNhomVatTuHangHoa.Focus();
                this.Text = "Thêm mới vật tư hàng hóa - dịch vụ";
                _DoiTuong = DanhMucVatTuHangHoa.Instance.NewDoiTuong();
                txtGiaMua.EditValue = (decimal)0;
                txtGiaBan.EditValue = (decimal)0;
                txtGiaBia.EditValue = (decimal)0;
                txtChietKhau.EditValue = (int)0;

                cmbTacGia.DataSource = DanhMucVatTuHangHoa.GetAllTacGia();
                cmbTacGia.DisplayMember = "TacGia";
                cmbTacGia.Text = "";

                cmbNhaXuatBan.DataSource = DanhMucVatTuHangHoa.GetAllNhaXuatBan();
                cmbNhaXuatBan.DisplayMember = "NhaXuatBan";
                cmbNhaXuatBan.Text = "";

                cmbNguonCungCap.DataSource = DanhMucVatTuHangHoa.GetAllNguonCungCap();
                cmbNguonCungCap.DisplayMember = "NguonCungCap";
                cmbNguonCungCap.Text = "";

                cmbDonViTinh.DataSource = DanhMucVatTuHangHoa.GetAllDonViTinh();
                cmbDonViTinh.DisplayMember = "DonViTinh";
                cmbDonViTinh.Text = "";

                _EditState = EditState.CreatingNew;
            }
            else
            {
                cmbNhomVatTuHangHoa.Focus();
                this.Text = "Sửa thông tin vật tư hàng hóa - dịch vụ";
                txtTen.Text = (_DoiTuong as VatTuHangHoa).Ten;
                cmbNhaXuatBan.Text = (_DoiTuong as VatTuHangHoa).NhaXuatBan;
                cmbNguonCungCap.Text = (_DoiTuong as VatTuHangHoa).NguonCungCap;
                cmbTacGia.Text = (_DoiTuong as VatTuHangHoa).TacGia;
                txtChietKhau.EditValue = (_DoiTuong as VatTuHangHoa).ChietKhau;
                chkSuDung.Checked = (_DoiTuong as VatTuHangHoa).TrangThai;
                chkKhongSuDung.Checked = !(chkSuDung.Checked);
                cmbNhomVatTuHangHoa.EditValue = (_DoiTuong as VatTuHangHoa).NhomVatTuHangHoa.ID;
                txtGiaBan.EditValue = (_DoiTuong as VatTuHangHoa).GiaBanHienTai;
                txtGiaMua.EditValue = (_DoiTuong as VatTuHangHoa).GiaMuaHienTai;
                txtGiaBia.EditValue = (_DoiTuong as VatTuHangHoa).GiaBia;
                txtMa.Text = (_DoiTuong as VatTuHangHoa).Ma;

                cmbTacGia.DataSource = DanhMucVatTuHangHoa.GetAllTacGia();
                cmbTacGia.DisplayMember = "TacGia";
                cmbTacGia.Text = (_DoiTuong as VatTuHangHoa).TacGia;

                cmbNhaXuatBan.DataSource = DanhMucVatTuHangHoa.GetAllNhaXuatBan();
                cmbNhaXuatBan.DisplayMember = "NhaXuatBan";
                cmbNhaXuatBan.Text = (_DoiTuong as VatTuHangHoa).NhaXuatBan;

                cmbNguonCungCap.DataSource = DanhMucVatTuHangHoa.GetAllNguonCungCap();
                cmbNguonCungCap.DisplayMember = "NguonCungCap";
                cmbNguonCungCap.Text = (_DoiTuong as VatTuHangHoa).NguonCungCap;

                cmbDonViTinh.DataSource = DanhMucVatTuHangHoa.GetAllDonViTinh();
                cmbDonViTinh.DisplayMember = "DonViTinh";
                cmbDonViTinh.Text = (_DoiTuong as VatTuHangHoa).DonViTinh;

                _EditState = EditState.Modifying;
            }
        }
        protected override bool CheckFullInfo()
        {
            if (txtMa.Text == "") throw new Exception("Chưa điền mã vật tư hàng hóa - dịch vụ");
            if (txtTen.Text == "") throw new Exception("Chưa điền tên vật tư hàng hóa - dịch vụ");
            if (cmbNhomVatTuHangHoa.EditValue == null) throw new Exception("Chưa chọn nhóm vật tư hàng hóa - dịch vụ");
            if (txtGiaBan.EditValue == null) throw new Exception("Chưa nhập giá bán mặc định");
            if (txtGiaMua.EditValue == null) throw new Exception("Chưa nhập giá mua mặc định");
            if (txtGiaBia.EditValue == null) throw new Exception("Chưa nhập giá bìa mặc định");
            return true;
        }

        protected override void SetDoiTuongProperties()
        {
            (_DoiTuong as VatTuHangHoa).Ma = txtMa.Text.Trim();
            (_DoiTuong as VatTuHangHoa).Ten = txtTen.Text.Trim();
            (_DoiTuong as VatTuHangHoa).TrangThai = chkSuDung.Checked;
            (_DoiTuong as VatTuHangHoa).TacGia = cmbTacGia.Text.Trim();
            (_DoiTuong as VatTuHangHoa).NguonCungCap = cmbNguonCungCap.Text.Trim();
            (_DoiTuong as VatTuHangHoa).NhaXuatBan = cmbNhaXuatBan.Text.Trim();
            (_DoiTuong as VatTuHangHoa).ChietKhau = (int)txtChietKhau.EditValue;
            (_DoiTuong as VatTuHangHoa).NhomVatTuHangHoa = DanhMucNhomVatTuHangHoa.Instance.GetByID((Guid)cmbNhomVatTuHangHoa.EditValue);
            (_DoiTuong as VatTuHangHoa).GiaBanHienTai = (decimal)txtGiaBan.EditValue;
            (_DoiTuong as VatTuHangHoa).GiaMuaHienTai = (decimal)txtGiaMua.EditValue;
            (_DoiTuong as VatTuHangHoa).GiaBia = (decimal)txtGiaBia.EditValue;
            (_DoiTuong as VatTuHangHoa).DonViTinh = cmbDonViTinh.Text.Trim();
        }

        private void chkSuDung_Click(object sender, EventArgs e)
        {
            chkKhongSuDung.Checked = false; ;
        }

        private void chkKhongSuDung_CheckedChanged(object sender, EventArgs e)
        {
            chkSuDung.Checked = false;
        }


        private void txtGiaBan_EditValueChanged(object sender, EventArgs e)
        {
            txtGiaBia.EditValue = txtGiaBan.EditValue;
        }

        private void MakeBarCode()
        {
            if (txtMa.Text == "")
                txtMa.Text = DanhMucVatTuHangHoa.GenBarCode((Guid)cmbNhomVatTuHangHoa.EditValue);
        }

        private void cmbNhomVatTuHangHoa_EditValueChanged(object sender, EventArgs e)
        {
            if (cmbNhomVatTuHangHoa.EditValue is Guid)
            {
                NhomVatTuHangHoa nhom = DanhMucNhomVatTuHangHoa.Instance.GetByID((Guid)cmbNhomVatTuHangHoa.EditValue);
                if (nhom != null)
                {
                    if (nhom.IsSach)
                    {
                        lblTitle.Text = "Sách";
                        lblGiaBia.Text = "Giá bìa";
                        //cmbTacGia.DropDownStyle = ComboBoxStyle.DropDownList; 
                        //cmbNhaXuatBan.DropDownStyle = ComboBoxStyle.DropDownList;
                    }
                    else
                    {
                        lblTitle.Text = "Hàng hóa khác sách";
                        lblGiaBia.Text = "Giá tại cửa hàng";
                        cmbNhaXuatBan.Text = "";
                        cmbTacGia.Text = "";
                        cmbNhaXuatBan.Enabled = false;
                        cmbTacGia.Enabled = false;

                    }
                }
                MakeBarCode();
            }
        }

        private void txtGiaBia_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void FrmEditHangHoa_Load(object sender, EventArgs e)
        {

        }

        private void lblTitle_Click(object sender, EventArgs e)
        {

        }

        private void txtChietKhau_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void cmbTacGia_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void cmbNhomVatTuHangHoa_Load(object sender, EventArgs e)
        {

        }

        private void txtMa_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void txtTen_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void cmbDonViTinh_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void cmbNguonCungCap_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void cmbNhaXuatBan_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void txtGiaMua_EditValueChanged(object sender, EventArgs e)
        {

        }
    }
}
